import 'dart:async';

import 'package:flutter/material.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';

class RefreshWrapper extends StatelessWidget {
  final EasyRefreshController controller;
  final void Function()? onRefresh;

  final void Function()? onLoad;
  final ERChildBuilder child;
  final _CIProperties _headerProperties = _CIProperties(
    name: 'Header',
    alignment: MainAxisAlignment.center,
    infinite: false,
  );
  final _CIProperties _footerProperties = _CIProperties(
    name: 'Footer',
    alignment: MainAxisAlignment.start,
    infinite: true,
  );
  RefreshWrapper({
    Key? key,
    required this.controller,
    this.onRefresh,
    this.onLoad,
    required this.child,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return EasyRefresh.builder(
        controller: controller,
        onRefresh: onRefresh,
        onLoad: onLoad,
        header: ClassicHeader(
          clamping: _headerProperties.clamping,
          backgroundColor: Colors.transparent,
          mainAxisAlignment: _headerProperties.alignment,
          showMessage: _headerProperties.message,
          showText: _headerProperties.text,
          infiniteOffset: _headerProperties.infinite ? 70 : null,
          triggerWhenReach: _headerProperties.immediately,
          dragText: '拉动刷新'.tr,
          armedText: '释放刷新'.tr,
          readyText: '刷新中...'.tr,
          processingText: '刷新中...'.tr,
          processedText: '刷新完成'.tr,
          noMoreText: '没有更多数据'.tr,
          failedText: '刷新失败'.tr,
          messageText: '最后更新于 %T'.tr,
        ),
        footer: ClassicFooter(
          clamping: _footerProperties.clamping,
          backgroundColor: Colors.transparent,
          mainAxisAlignment: _footerProperties.alignment,
          showMessage: _footerProperties.message,
          showText: _footerProperties.text,
          infiniteOffset: _footerProperties.infinite ? 70 : null,
          triggerWhenReach: _footerProperties.immediately,
          dragText: '拉动加载'.tr,
          armedText: '释放加载'.tr,
          readyText: '加载中...'.tr,
          processingText: '加载中...'.tr,
          processedText: '加载完成'.tr,
          noMoreText: '没有更多数据'.tr,
          failedText: '加载失败'.tr,
          messageText: '最后更新于 %T'.tr,
        ),
        childBuilder: child,
    );
  }
}
class _CIProperties {
  final String name;
  bool disable = false;
  bool clamping = false;
  bool background = false;
  MainAxisAlignment alignment;
  bool message = true;
  bool text = true;
  bool infinite;
  bool immediately = false;

  _CIProperties({
    required this.name,
    required this.alignment,
    required this.infinite,
  });
}
